//
//  GFXNetworkHandle.m
//  zijiaosuo
//
//  Created by FXGuo on 16/5/24.
//  Copyright © 2016年 guoguo. All rights reserved.
//

#import "GFXNetworkHandle.h"
#import "NetworkManager.h"
#import "MBProgressHUD+MJ.h"

@implementation GFXNetworkHandle

+ (instancetype)shareHandle {
    static GFXNetworkHandle *_sharedHandle = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedHandle = [[GFXNetworkHandle alloc] init];
    });
    return _sharedHandle;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.networkError = NO;
    }
    return self;
}
- (void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure
{
    if (self.networkError == YES) {
        if (failure) {
            NSError *error = [NSError errorWithDomain:NoNetworkStatus code:404 userInfo:nil];
            failure(error);
        }
        return;
    }
    [NetworkManager get:url params:params success:success failure:failure];
}
- (void)post:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure
{
    if (self.networkError == YES) {
        if (failure) {
            NSError *error = [NSError errorWithDomain:NoNetworkStatus code:404 userInfo:nil];
            failure(error);
        }
        return;
    }
    [NetworkManager post:url params:params success:success failure:failure];
}
- (void)post:(NSString *)url params:(NSDictionary *)params constructingBodyWithBlock:(void (^)(id formData))block progress:(void (^)(NSProgress *progress))progress success:(void (^)(id json))success failure:(void (^)(id json))failure
{
    if (self.networkError == YES) {
        //[MBProgressHUD showError:NoNetworkStatus];
        if (failure) {
            NSError *error = [NSError errorWithDomain:NoNetworkStatus code:404 userInfo:nil];
            failure(error);
        }
        return;
    }
    [NetworkManager post:url params:params constructingBodyWithBlock:block progress:progress success:success failure:failure];
}

+ (void)startMonitoring
{
    // 1.获得网络监控的管理者
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    // 2.设置网络状态改变后的处理
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        // 当网络状态改变了, 就会调用这个block
        switch (status)
        {
            case AFNetworkReachabilityStatusUnknown: // 未知网络
                [GFXNetworkHandle shareHandle].networkError = NO;
                break;
            case AFNetworkReachabilityStatusNotReachable: // 没有网络(断网)
                [GFXNetworkHandle shareHandle].networkError = YES;
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN: // 手机自带网络
                [GFXNetworkHandle shareHandle].networkError = NO;
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi: // WIFI
                [GFXNetworkHandle shareHandle].networkError = NO;
                break;
        }
    }];
    [mgr startMonitoring];
}
//+ (void)cancelAllNetItems
//{
//    
//}

@end
